import time
from appium.webdriver.common.appiumby import AppiumBy
from app.wework.model.member import Member
from app.wework.pages.edit_contact_page import EditContactPage
from app.wework.model.base_page import BasePage


class ContactsPage(BasePage):
    def __init__(self, driver=None):
        super().__init__(driver)

    def search(self, keyword):
        """通讯录搜索"""
        self.find(AppiumBy.ID, value='kv_').click()
        self.find(AppiumBy.ID, value='j6i').send_keys(keyword)
        e_list = self.driver.find_elements(AppiumBy.XPATH, '//*[contains(@resource-id, "g9n")]/*[contains(@class, '
                                                           '"Text")]')
        name_list = []
        if len(e_list) == 0:
            return name_list
        else:
            for name in e_list:
                name_list.append(name.text)
            return name_list

    def search_back(self):
        self.find(AppiumBy.ID, value='kuf').click()

    def add_member(self, member: Member):
        """添加成员"""
        # 点击添加成员
        self.find(AppiumBy.CSS_SELECTOR, '[text="添加成员"]').click()
        # 点击手动输入添加
        self.find(AppiumBy.CSS_SELECTOR, '[text="手动输入添加"]').click()
        # 输入姓名、手机号
        self.find(AppiumBy.ID, 'buv').send_keys(member.name)
        self.find(AppiumBy.ID, 'hvk').send_keys(member.phone)
        # 取消勾选【保存后自动发送邀请通知】
        self.find(AppiumBy.ID, 'hfi').click()
        # 点击保存
        self.find(AppiumBy.ID, 'avy').click()
        time.sleep(3)
        # 点击返回回到通讯录页面
        self.find(AppiumBy.ID, 'kuf').click()

    def add_customer(self):
        """添加客户"""
        ...

    def my_customer(self):
        """我的客户"""
        ...

    def manage_contacts(self):
        """管理通讯录"""
        ...

    def edit_contect(self, keyword):
        """编辑联系人"""
        self.find(AppiumBy.ID, 'kv_').click()
        self.find(AppiumBy.ID, 'j6i').send_keys(keyword)
        self.find(AppiumBy.ID, 'ezv').click()
        self.find(AppiumBy.ID, 'kuz').click()
        self.find(AppiumBy.ID, 'c2y').click()
        return EditContactPage(self.driver)
